﻿#if Spine
using Spine.Unity;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine;


public class SpinePlayerController : MonoBehaviour
{

    public float speed = 5;
    public SkeletonAnimation skeletonAnimation;
    public Spine.AnimationState spineAnimationState;
    public Spine.Skeleton skeleton;

    //private string horizontalAxis = "Horizontal";
    //private string attackButton = "Fire1";
    //private string jumpButton = "Jump";
    //private string vertical = "Vertical";

    // Use this for initialization
    void Start()
    {
        skeletonAnimation = GetComponent<SkeletonAnimation>();
        spineAnimationState = skeletonAnimation.AnimationState;
        skeleton = skeletonAnimation.Skeleton;
        spineAnimationState.AddAnimation(0, "idle", true, 0);
    }

    // Update is called once per frame
    void Update()
    {
        float newSpeed = speed * Time.deltaTime;
        float curSpeed = 0f;
        float xieFen = 1.2f;
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S))
        {
            curSpeed = 1;

            TrackEntry e0 = spineAnimationState.GetCurrent(0);
            if (e0.animation.Name != "walk")
            {
                TrackEntry e1 = spineAnimationState.SetAnimation(0, "walk", true);
            }
        }

        if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.S))
        {
            skeletonAnimation.skeleton.ScaleX = 1;
            transform.Translate(-newSpeed / xieFen, -newSpeed / xieFen, 0);
        }
        else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S))
        {
            skeletonAnimation.skeleton.ScaleX = -1;
            transform.Translate(newSpeed / xieFen, -newSpeed / xieFen, 0);
        }
        else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
        {
            skeletonAnimation.skeleton.ScaleX = 1;
            transform.Translate(-newSpeed / xieFen, newSpeed / xieFen, 0);
        }
        else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
        {
            skeletonAnimation.skeleton.ScaleX = -1;
            transform.Translate(newSpeed / xieFen, newSpeed / xieFen, 0);
        }
        else if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(-newSpeed, 0, 0);
            skeletonAnimation.skeleton.ScaleX = 1;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(0, -newSpeed, 0);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(newSpeed, 0, 0);
            skeletonAnimation.skeleton.ScaleX = -1;
        }
        else if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(0, newSpeed, 0);
        }

        if (Input.GetKeyUp(KeyCode.J))
        {
            TrackEntry e = spineAnimationState.GetCurrent(0);
            TrackEntry e1 = spineAnimationState.SetAnimation(0, "attack", true);
            e1.loop = false;
            spineAnimationState.AddAnimation(0, "idle", true, 0);
        }
        else if (Input.GetKeyUp(KeyCode.K))
        {
            spineAnimationState.SetAnimation(0, "attack", true);
        }
        else if (Input.GetKeyUp(KeyCode.L))
        {
            spineAnimationState.SetAnimation(0, "attack", true);
        }
        else if (curSpeed == 0)
        {
            TrackEntry e0 = spineAnimationState.GetCurrent(0);
            if (e0.Animation.name != "idle" && e0.IsComplete)
            {
                spineAnimationState.SetAnimation(0, "idle", true);
                return;
            }
        }


    }
}

#endif
